/**
 * @author: welling
 * okey007@163.com	19880812
 */
Weifox.apps.fanfou = {
	name:'fanfou',
	domain:'http://api.fanfou.com/',
	icon:'',
	appKey:'Weifox',
	method:{
		basic:1
	},
	timeline: {
		friends_timeline: {label: '首页'	},
		mentions: {label: '@我的'},
		direct_messages: {label: '私信'},
		user_timeline: {label: '我的空间'},
		public_timeline:{label: '随便看看'}
	},
	/***
	 * 发微博
	 * @param {Object} user
	 * @param {Object} data
	 */
	send:function(user,data){
		
		var req={};
//		var encode=Weifox.net.rfcEncode;
		var net=Weifox.net;
		switch(data.type){
			case 'nl':
				req.url='http://api.fanfou.com/statuses/update.json';
				req.args=[['status', data.text]];
				break;
			case 're-cm':
			case 'cm':
				req.url='http://api.fanfou.com/statuses/update.json';
//				data.text=encode('@'+data.sender)+' '+data.text;
				req.args=[['status', data.text],['in_reply_to_status_id',data.id]];
				break;
			case 'rp':
				req.url='http://api.fanfou.com/statuses/update.json';
//				data.text=data.text+' '+"%E8%BD%AC%3A%40"+encode(data.sender+' '+data.content);
				req.args=[['status', data.text],['repost_status_id',data.id]];
				break;
			case 're-dm':
				req.url='http://api.fanfou.com/direct_messages/new.json';
				req.args=[['text', data.text],['user',data.id]];
				req.args.push(['in_reply_to_id',data.in_reply_to_id ]);
				break;
			case 'nl-pic':
				req.url='http://api.fanfou.com/photos/upload.json';
//				req.args=[['status', net.JSStringToUTF8( net.rfcDecode(data.text))]];
				req.args=[['status', data.text]];
				req.stream={
					photo:data.pic
				}
				break;
			default:
				return;
		}
//		Weifox.debug.showObj(data);
		this.sendRequest(user,req);
	},
	/**
	 * 
	 * @param {Object} method 
	 * @param {Object} para
	 * @param {Object} callback	返回一个User Object
	 * user_name user_id user_icon para1 para2 method enable 
	 * timeline{
	 * 	friends.. xx.. xx..
	 * } 
	 */
	parseText:function(node,text){
		this.processLinkText(node,text);
	},
	processLinkText: function(node, str){
        //first process the " &quot;
        var pattern = /((http(s?))\:\/\/)([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/([\w#!:.?+=&%@~*;,\-\/\$])*)?/g;
		//var photo=/http:\/\/fanfou.com\/photo\/[0-9a-zA-Z]/;
		while (pattern.exec(str) != null) {
            var left = RegExp.leftContext;
            var right = RegExp.rightContext;
            var url = RegExp.lastMatch;
            
            this.processTopicText(node, left);//将左边的字符串加入子节点
            var shorturl = url;
            if (url.length >= 25) {//如果比这个长就截断它
                shorturl = shorturl.substring(0, 22);
                shorturl += '...';
            }
			/*
			if(photo.exec(right)!=null)
            	node.appendChild(Weifox.ui.createLink(url, shorturl,'fanfou-pic'));//将中间匹配的字符串创建成链接加入子节点
            else*/ node.appendChild(Weifox.ui.createLink(url, shorturl,'link'));
			
			str = right;
            pattern.lastIndex = 0;//reset.
        }
        //将剩下的str右边加入子节点
        this.processTopicText(node, str);
    },
 
    processTopicText: function(node, str){
        var pattern = /#([\+0-9". a-zA-Z_\u4E00-\u9FA5~])+#/;//识别 #xxx#
        while (pattern.exec(str) != null) {
            var left = RegExp.leftContext;
            var right = RegExp.rightContext;
            var m = RegExp.lastMatch;
            
            node.appendChild(document.createTextNode(left));
            if (m != '')
                node.appendChild(Weifox.ui.createLink('http://fanfou.com/q/' + encodeURI(m.substring(1, m.length - 1)), m));
            
            str = right;
            pattern.lastIndex = 0;//reset.			
        }
		var c = document.createTextNode(str);
		node.appendChild(c);
    },	
	parsePic:function(text){
		var pattern=/http:\/\/fanfou.com\/photo\/[a-zA-Z0-9]*/g;
		var url=null;
		while (pattern.exec(text) != null) {
            url=RegExp.lastMatch;
        }
		return url;
	},
	verify:function(method,para,callback){
		if(method=='basic'){
			var data={
				url:'http://api.fanfou.com/account/verify_credentials.json',
				success:function(u){
					callback({
						app:'fanfou',
						method:'basic',
						para1:para.un,
						para2:para.pw,
						user_id:u.id,
						user_name:u.screen_name,
						user_icon:u.profile_image_url
					});
				},
				error:callback
			}
			Weifox.net.makeBasic(data,para.un,para.pw);
			new Weifox.net.Request().send(data);
		}else if(method=='oauth'){////oauth
		}
	},
	handleError:function(err){
		//Weifox.debug.showObj(err);
		//alert('Error fanfou:'+new String(err.content));
	},
	/**
	 * 
	 * 返回一个封好数据的Request,直接Send即可,
	 * Data需先设置好再传入,否则OAuth的签名计算会出错.
	 * 适用于已授权的API
	 * @param {Object} user
	 * @param {Object} data
	 */
	sendRequest:function(user,data){
		
		var method=user.method.toUpperCase();
		if(method=='BASIC'){
			if(!data.args){
				data.args=[];
			}
			data.args.push(['source',this.appKey]);
			
			Weifox.net.makeBasic(data,user.para1,user.para2);
		}else if(method=='OAUTH'){
			throw new Error('This app does not support OAuth!');
		}else{
			throw new Error('This app does not support '+method);
		}
		var req=new Weifox.net.Request();
		data.error=this.handleError;
		req.send(data);
	}	
}